﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Drawing;

namespace TeachMe.Application.Drawing
{
    public class ProgressDrawingContext
    {
        private int _cachedHeight = -1;
        private int _cachedWidth = -1;

        public Image Image { get; set; }
        public Rectangle Rectangle { get; set; }
        public int MiddleFragmentStart { get; set; }
        public int RightFragmentStart { get; set; }
        public float Segment { get; set; }
        public ImageExtendMode ExtendMode { get; set; }

        private int GetHeight()
        {
            if (_cachedHeight < 0)
            {
                _cachedHeight = Image.Height;
            }

            return _cachedHeight;
        }

        private int GetWidth()
        {
            if (_cachedWidth < 0)
            {
                _cachedWidth = Image.Width;
            }

            return _cachedWidth;
        }

        private int LeftFragmentWidth
        {
            get
            {
                return MiddleFragmentStart;
            }
        }
        
        private int MiddleFragmentWidth
        {
            get
            {
                return RightFragmentStart - MiddleFragmentStart;
            }
        }

        private int RightFragmentWidth
        {
            get
            {
                return Image.Width - RightFragmentStart;
            }
        }

        public void Draw(Graphics graphics, int progress)
        {
            Rectangle destination = new Rectangle(Rectangle.X, Rectangle.Y, LeftFragmentWidth, GetHeight());
            Rectangle source = new Rectangle(0, 0, LeftFragmentWidth, GetHeight());
            graphics.DrawImage(Image, destination, source, GraphicsUnit.Pixel);

            int currentEnd = destination.Right;

            source = new Rectangle(MiddleFragmentStart, 0, MiddleFragmentWidth, GetHeight());

            switch (ExtendMode)
            {
                case ImageExtendMode.Stretch:
                    destination = new Rectangle(currentEnd, Rectangle.Y, (int)(Segment * progress), GetHeight());
                    graphics.DrawImage(Image, destination, source, GraphicsUnit.Pixel);
                    currentEnd = destination.Right;
                    break;
                case ImageExtendMode.Multiply:
                    for (int idx = 0; idx < (int)(Segment * progress); idx++)
                    {
                        destination = new Rectangle(currentEnd, Rectangle.Y, MiddleFragmentWidth, GetHeight());
                        graphics.DrawImage(Image, destination, source, GraphicsUnit.Pixel);
                        currentEnd += MiddleFragmentWidth;
                    }
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            destination = new Rectangle(currentEnd, Rectangle.Y, RightFragmentWidth, GetHeight());
            source = new Rectangle(RightFragmentStart, 0, GetWidth() - RightFragmentStart, GetHeight());
            graphics.DrawImage(Image, destination, source, GraphicsUnit.Pixel);
        }
    }
}
